package com.azt.provider.impl;

import com.azt.api.pojo.MemberApply;
import com.azt.api.pojo.MemberApplyReq;
import com.azt.api.pojo.SmsParam;
import com.azt.api.service.MemberApplyService;
import com.azt.api.service.MqProducerService;
import com.azt.api.service.MsgConfigService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.MemberApplyMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MemberApplyServiceImpl implements MemberApplyService {

	@Autowired
	private MemberApplyMapper memberApplyMapper;

	@Autowired
	private MsgConfigService msgConfigService;

	@Override
	public Pagination getMemberApplyPage(int pageno, int pagesize, Map<String, String> searchMap){
		
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		PageHelper.orderBy("auditState asc, createtime desc");
		List<MemberApply> memberApplyList = memberApplyMapper.selectAll();
		Pagination p = new Pagination(startPage, memberApplyList);
		p.setList(memberApplyList);
		p.setParams(searchMap);
		return p;
	}
	@Override
	public List<MemberApply> queryListByIds(String ids){
		List<MemberApply> list = memberApplyMapper.selectByIds(ids);
		return list;
		
	}
	@Override
	public void update(MemberApply ma) {
		memberApplyMapper.updateByPrimaryKeySelective(ma);
	}

	@Override
	public void save(MemberApplyReq req) {
		MemberApply apply = new MemberApply();
		apply.setAuditState(0);
		BeanUtils.copyProperties(req, apply);
		memberApplyMapper.insert(apply);
		// 发送短信通知运营
		Map<String, String> params = new HashMap<>();
		params.put("companyName", apply.getCompanyName());
		params.put("contacts", apply.getContacts());
		params.put("tel", apply.getTel());
		msgConfigService.sendMessage("MEMBER_APPLY", params);
	}
}
